package cn.hackcloud.javabase.juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestLock {
	public static void main(String[] args) {
		Ticket ticket = new Ticket();
		new Thread(ticket, "1号").start();
		new Thread(ticket, "2号").start();
		new Thread(ticket, "3号").start();
	}
}

class Ticket implements Runnable {
	private int ticket = 100;
	private Lock lock = new ReentrantLock();

	@Override
	public void run() {
		while (true) {
			lock.lock();
			try {
				if (ticket > 0) {
					Thread.sleep(100);
					System.err.println(Thread.currentThread().getName() + ":" + --ticket);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
		}

	}

}